package br.com.virtualsoluction.dao;

import br.com.virtualsoluction.bo.ItemVenda;
import br.com.virtualsoluction.bo.Venda;
import br.com.virtualsoluction.utilitarios.ConnectionFactory;
import java.sql.*;
import java.sql.ResultSet;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DAOItemVenda implements CRUD<ItemVenda>{

    private String sql;
    private Connection c;
    private PreparedStatement s;
    private ResultSet rs;

    public DAOItemVenda(){
        this.sql = "";
        this.c = new ConnectionFactory().getConnection();
        this.s = null;
        this.rs = null;
    }

    public boolean insert(Venda elemento) {
        int i,x;
        boolean b=false;
        
        i = elemento.getIdVenda(); //getChave();

        for (x = 0; x < elemento.getItemVenda().size(); x++) {            
            this.sql = " insert into itemvenda (vendaid, seqitem, produtoid, qtd, preco, desconto) ";
            this.sql += " values ( " + String.valueOf(i) + " , "+
                                       String.valueOf(x+1)+" , "+
                                       elemento.getItemVenda().get(x).getProduto().getCodigo()+" , "+
                                       elemento.getItemVenda().get(x).getQuantidade()+" , "+
                                       elemento.getItemVenda().get(x).getPreco()+" , "+
                                       elemento.getItemVenda().get(x).getDesconto()+" ) ";

            try {
                this.s = this.c.prepareStatement(this.sql);                
                this.s.execute();
                
            } catch (SQLException ex) {
                return false;
            }          
        }
        return true;
    }

    public boolean update(ItemVenda elemento) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean delete(ItemVenda elemento) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public ItemVenda search(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<ItemVenda> getList() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getChave() {
        String vSQL ="";
        int ret = 0;

        vSQL = "select gen_id(gen_itemvenda_id,1) from rdb$database";
        try {
            this.s = this.c.prepareStatement(vSQL);
        } catch (SQLException ex) {
            Logger.getLogger(DAOItemVenda.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            this.rs = this.s.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(DAOItemVenda.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            rs.next();
            ret = rs.getInt("gen_id");
        } catch (SQLException ex) {
            Logger.getLogger(DAOItemVenda.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ret;
    }

    public boolean insert(ItemVenda elemento) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}